package display;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import term.dept.Department;
import term.dept.Department.Code;
import term.dept.course.Course;
import term.dept.course.section.Section;

import adoption.Adoption;

public class AdoptionControl extends JFrame {

	private JPanel contentPane;
	private InventoryData inv;
	private JTable adoptionTable;
	private JComboBox deptBox, courseBox, sectionBox;
	private Department curDept;
	private Course curCourse;
	private Section curSection;
	private AdoptionActionListener aal = new AdoptionActionListener();
	/**
	 * Create the frame.
	 */
	public AdoptionControl(InventoryData in) {
		
		inv = in;
		
		setTitle("Adoptions");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{424, 424, 424, 424, 424, 0};
		gbl_contentPane.rowHeights = new int[]{23, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		JButton massImport = new JButton("Import");
		
		massImport.addActionListener(aal);
		
		GridBagConstraints gbc_massImport = new GridBagConstraints();
		gbc_massImport.insets = new Insets(0, 0, 5, 5);
		gbc_massImport.anchor = GridBagConstraints.NORTH;
		gbc_massImport.fill = GridBagConstraints.HORIZONTAL;
		gbc_massImport.gridx = 2;
		gbc_massImport.gridy = 0;
		contentPane.add(massImport, gbc_massImport);
		
		deptBox = new JComboBox();
		deptBox.setModel(new DefaultComboBoxModel(Code.values()));
		GridBagConstraints gbc_deptBox = new GridBagConstraints();
		gbc_deptBox.insets = new Insets(0, 0, 5, 5);
		gbc_deptBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_deptBox.gridx = 0;
		gbc_deptBox.gridy = 1;
		contentPane.add(deptBox, gbc_deptBox);
		
		courseBox = new JComboBox();
		GridBagConstraints gbc_courseBox = new GridBagConstraints();
		gbc_courseBox.insets = new Insets(0, 0, 5, 5);
		gbc_courseBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_courseBox.gridx = 1;
		gbc_courseBox.gridy = 1;
		contentPane.add(courseBox, gbc_courseBox);
		
		sectionBox = new JComboBox();
		GridBagConstraints gbc_sectionBox = new GridBagConstraints();
		gbc_sectionBox.insets = new Insets(0, 0, 5, 5);
		gbc_sectionBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_sectionBox.gridx = 2;
		gbc_sectionBox.gridy = 1;
		contentPane.add(sectionBox, gbc_sectionBox);
		
		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 5;
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 2;
		contentPane.add(scrollPane, gbc_scrollPane);
		
		adoptionTable = new JTable();
		adoptionTable.setAutoCreateRowSorter(true);
		adoptionTable.setFillsViewportHeight(true);
		adoptionTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		adoptionTable.setModel(emptyTable());
		scrollPane.setViewportView(adoptionTable);
		
		deptBox.addActionListener(aal);
		courseBox.addActionListener(aal);
		sectionBox.addActionListener(aal);
		
		aal.actionPerformed(null);
	}
	
	private DefaultTableModel emptyTable() {
		DefaultTableModel dtm = new DefaultTableModel(
			new Object[][] {{null, null, null, null, null},},
			new String[] {"Author", "Title", "Status", "ISBN", "Inv Num"}
			) {
				Class[] columnTypes = new Class[] {
					String.class, String.class, String.class, String.class, String.class
				};
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
			};
				
		return dtm;
	}

	private class AdoptionActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {

			if (arg0 != null && arg0.getActionCommand().equalsIgnoreCase("import")) {
				AdoptionImport ai = new AdoptionImport(inv);
				ai.setVisible(true);
				return;
			}

			Code code = (Code)deptBox.getSelectedItem();
			
			if (curDept == null || curDept.getCode() != code)
				courseBox.setModel(getCourses(code));
			
			Course course = (Course)courseBox.getSelectedItem();
			
			if ((curCourse == null || curCourse != course) && course != null)
				sectionBox.setModel(getSections(course));

			Section section = (Section)sectionBox.getSelectedItem();
			
			if ((curSection == null || curSection != section) && section != null)
				getAdoptions(section);
			
			/*TableColumnModel cm = adoptionTable.getColumnModel();
			cm.getColumn(0).setWidth(65);
			cm.getColumn(1).setWidth(155);
			cm.getColumn(2).setWidth(15);
			cm.getColumn(3).setWidth(55);
			cm.getColumn(4).setWidth(15);*/
			//"Author", "Title", "Status", "ISBN", "Inv Num"
		}
		
		private DefaultComboBoxModel getCourses(Code selected) {

			curDept = inv.getCurTerm().getDept(selected);
			
			Map<Integer,Course> courseMap = curDept.getCourses();
			Course[] courseArray;
			if (courseMap.isEmpty()) {
				courseArray = new Course[]{null};
			}
			else {
				int i = 0;
				courseArray = new Course[courseMap.size()];
				List<Integer> crsList = new ArrayList<Integer>(courseMap.keySet());
				Collections.sort(crsList);
				for (Integer crsNum : crsList)
					courseArray[i++] = courseMap.get(crsNum);
			}
			return new DefaultComboBoxModel(courseArray);
		}
		
		private DefaultComboBoxModel getSections(Course selected) {

			//set course number label
			curCourse = selected;
			
			Map<String,Section> secs = curCourse.getSections();
			Section[] sectionArray;
			
			if (secs.isEmpty())
				sectionArray = new Section[]{null};
			else {
				sectionArray = new Section[secs.size()];
				List<String> secList = new ArrayList<String>(secs.keySet());
				Collections.sort(secList);
				int i = 0;
				for (String s : secList)
					sectionArray[i++] = secs.get(s);
			}
			
			return new DefaultComboBoxModel(sectionArray);
		}
		
		private void getAdoptions(Section selected) {
			
			if (curSection == null || curSection != selected) {
				//retrieve adoptions
				curSection = selected;
				List<Adoption> adoptions = curSection.getAdoptions();
				
				if (adoptions.isEmpty())
					return;
				else {
					DefaultTableModel adoptionData = (DefaultTableModel)adoptionTable.getModel();
					for (; 0 < adoptionData.getRowCount();)
						adoptionData.removeRow(0);
					for (Adoption a : adoptions)
						adoptionData.addRow(new String[] {a.getBook().getAuthor(), a.getBook().getTitle(), a.getStatus(), String.valueOf(a.getBook().getISBN()), a.getBook().getInv_num()});
				}
			}
		}
		
	}
}
